package com.itheima.paydemo.Ali;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.response.AlipayTradePayResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author by jojo
 * @Date 2022/4/8
 * @Description
 */
@RestController("alipay")
public class AliPayController {


    @GetMapping("pay")
    public void pay(){
        try {
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                    "app_id",
                    "your private_key",
                    "json",
                    "GBK",
                    "alipay_public_key",
                    "RSA2");
            AlipayTradePayRequest request = new AlipayTradePayRequest();
            JSONObject bizContent = new JSONObject();
            bizContent.put("out_trade_no", "20210817010101001");
            bizContent.put("total_amount", 0.01);
            bizContent.put("subject", "测试商品");
            bizContent.put("scene", "bar_code");
            bizContent.put("auth_code", "28763443825664394");
            request.setBizContent(bizContent.toString());
            AlipayTradePayResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
    }
}
